//
//  InstallationDetailTabInverterNumberCell.swift
//  ezyform
//
//  Created by MoennigQ on 7/4/20.
//  Copyright © 2020 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SDWebImage
class InstallationDetailTabInverterNumberCell: UITableViewCell {
    let inverterNumberImageView = UIImageView()
    
    let inverterNumberLabel: BaseLabel = BaseLabel()
    let localFlagImageView = UIImageView()
    let activityView = UIActivityIndicatorView()
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    func setUpviews(){
        self.backgroundColor = .clear
        //seq no
        inverterNumberImageView.drawCorner(byRoundingCorners: .allCorners,radii: 3)
        inverterNumberImageView.contentMode = .scaleAspectFill
        inverterNumberImageView.clipsToBounds = true
        self.contentView.addSubview(inverterNumberImageView)
        inverterNumberImageView.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.left.equalToSuperview()
            make.width.height.equalTo(64)
            make.bottom.equalTo(-12)
        }
        // 停止后，隐藏菊花
        activityView.hidesWhenStopped = true
        //Style: whiteLarge比较大的白色环形进度条;white白色环形进度条;gray灰色环形进度条
        activityView.style = UIActivityIndicatorView.Style.white
        self.contentView.addSubview(activityView)
        activityView.snp.makeConstraints { (make) in
            make.center.equalTo(inverterNumberImageView)
        }
        //localFlag
        self.localFlagImageView.image = UIImage(named: "uploadImageFailed")
        self.contentView.addSubview(localFlagImageView)
        localFlagImageView.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(inverterNumberImageView)
            make.size.equalTo(CGSize(width: 16, height: 11))
        }
        
        //panelNumber
        inverterNumberLabel.textColor = UIColor(hexStr: "#292D32")
        inverterNumberLabel.setFont(R.font.interMedium(size: 15))
        inverterNumberLabel.textAlignment = .left
        inverterNumberLabel.numberOfLines = 0
        inverterNumberLabel.adjustsFontSizeToFitWidth = true
        self.contentView.addSubview(inverterNumberLabel)
        inverterNumberLabel.snp.makeConstraints { (make) in
            make.top.equalTo(inverterNumberImageView)
            make.left.equalTo(inverterNumberImageView.snp.right).offset(12)
            make.right.equalToSuperview().offset(-5)
            make.width.greaterThanOrEqualTo(0)
            make.height.lessThanOrEqualTo(64)
        }
        
        //line view
        let lineView = View(hexStr: Constant.SeparatorColorHexStr)
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(inverterNumberImageView)
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    func setData(_ inverterModel: PanelNumberImageDBModel){
        inverterNumberLabel.text = inverterModel.code
        //不符合panel number的校验规则，code显示为红色
        if !Tool().isValidInverterNumber(str: inverterModel.code) {
            self.inverterNumberLabel.textColor = .red
        }else{
            self.inverterNumberLabel.textColor = UIColor(hexStr: "#292D32")
        }
        let imageFilePath = inverterModel.image
        //operType为空，表示是server 的数据
        if inverterModel.operType == "" {
            //设置inverter number的已上传图片
            self.localFlagImageView.image = UIImage(named: "UploadImageSuccess")
            self.inverterNumberImageView.image = nil
            if imageFilePath != "" {
                self.inverterNumberImageView.backgroundColor = .lightGray
                guard let url = URL(string: imageFilePath) else {
                    return
                }
                self.activityView.startAnimating()
                self.inverterNumberImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "installation_detail_panelnumber_list_loadfaild"), options: [SDWebImageOptions.scaleDownLargeImages]) { image, error, cashtype, url in
                    self.activityView.stopAnimating()
                }
            }else{
                self.inverterNumberImageView.image = UIImage(named: "installation_detail_panelnumber_list_nopic")
            }
        }else{//本地数据
            self.localFlagImageView.image = UIImage(named: "uploadImageFailed")
            if imageFilePath.contains("http"), let url = URL(string: imageFilePath){
                self.activityView.startAnimating()
                self.inverterNumberImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "installation_detail_panelnumber_list_loadfaild"), options: [SDWebImageOptions.scaleDownLargeImages]) { image, error, cashtype, url in
                    self.activityView.stopAnimating()
                }
            }else if let image = UIImage(contentsOfFile: imageFilePath){
                self.inverterNumberImageView.image = image.generateThumbnailImage()
            }else{
                self.inverterNumberImageView.image = UIImage(named: "installation_detail_panelnumber_list_nopic")
            }
        }
    }
}
